package ${table.packageNames.controller};

<#list table.importPackages.controller as pkg>
import ${pkg};
</#list>

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Controller
@RequestMapping(value = "/${table.capitalEntityName}")
public class ${table.controllerName} {

<#if isGenerateTemplate>
    private String prefixView = "${pathPrefix}";
</#if>
    <#if isControllerCRUD>
    @Resource
    private ${table.serviceName} ${table.capitalServiceName};

    /**
     * 获取JSON 数据的list集合
     *
     * @param ${table.capitalEntityName} 对象参数
     * @return JSON格式的 对象
     */
    @GetMapping(value = "/get${table.entityName}List")
    @ResponseBody
    public ReturnResult get${table.entityName}List(${table.entityName} ${table.capitalEntityName}) {
        List<${table.entityName}> list = ${table.capitalServiceName}.findListByEntity(${table.capitalEntityName});
        return ReturnResult.ok(list);
    }

    /**
     * 添加数据
     *
     * @param ${table.capitalEntityName} 对象
     * @return JSON格式的 对象
     */
    @PostMapping(value = "/add${table.entityName}")
    @ResponseBody
    public ReturnResult add${table.entityName}(${table.entityName} ${table.capitalEntityName}) {
        // 添加成功则返回true
        if (${table.capitalServiceName}.saveNotEmpty(${table.capitalEntityName})) {
            return ReturnResult.ok();
        }
        return ReturnResult.error("添加失败.");
    }


    /**
     * 修改数据
     *
     * @param ${table.capitalEntityName} 对象
     * @return JSON格式的 对象
     */
    @PostMapping(value = "/update${table.entityName}")
    @ResponseBody
    public ReturnResult update${table.entityName}(${table.entityName} ${table.capitalEntityName}) {
        // 根据id修改，修改成功则返回true
        if (${table.capitalServiceName}.updateNotEmptyById(${table.capitalEntityName})) {
            return ReturnResult.ok();
        }
        return ReturnResult.error("修改失败.");
    }

    /**
     * 根据id删除
     *
     * @param id id
     * @return JSON格式的 对象
     */
    @PostMapping(value = "/delete${table.entityName}ById/{id}")
    @ResponseBody
    public ReturnResult delete${table.entityName}ById(@PathVariable Integer id) {
        // 如果id为null 为了安全考虑，则不执行，返回提示
        if (id == null) {
            return ReturnResult.error("Id不能为空!");
        } else {
            // 根据id删除，删除成功则返回true
            if (${table.capitalServiceName}.deleteById(id)) {
                return ReturnResult.ok("删除成功.");
            }
        }
        return ReturnResult.error("删除失败.");
    }

    /**
     * 根据传入id获取单个对象
     *
     * @param id id
     * @return JSON格式的 对象
     */
    @PostMapping(value = "/get${table.entityName}ById/{id}")
    @ResponseBody
    public ReturnResult get${table.entityName}ById(@PathVariable Integer id) {
        // 如果id为null 为了安全考虑，则不执行，返回提示
        if (id == null) {
            return ReturnResult.error("Id不能为空!");
        } else {
            return ReturnResult.ok(${table.capitalServiceName}.get${table.entityName}ById(id));
        }
    }
        <#if isPageMethod>
    /**
     * 分页查询
     *
     * @param ${table.capitalEntityName} 实体对象
     * @param pageIndex            起始页
     * @param pageSize             每页页数
     * @return 返回layui table格式数据
     */
    @GetMapping(value = "/queryTableData")
    @ResponseBody
    public ReturnResult queryTableData(${table.entityName} ${table.capitalEntityName}, Integer pageIndex, Integer pageSize) {
        // 查询分页数据
        PageInfo<${table.entityName}> pageInfo = ${table.capitalServiceName}.queryTableData(${table.capitalEntityName}, pageIndex, pageSize);
        return new ReturnResult(ReturnResult.SUCCESS, pageInfo.getList(), "成功.", pageInfo.getTotal());
    }
        </#if>

    <#if isGenerateTemplate>
    /**
     * 跳转到查询页面
     *
     * @return 逻辑视图
     */
    @GetMapping(value = "/goSelectView")
    public String goSelectView() {
        return prefixView + "${table.selectViewName}";
    }

    /**
     * 跳转到添加页面
     *
     * @return 逻辑视图
     */
    @GetMapping(value = "/goSaveView")
    public String goSaveView() {
        return prefixView + "${table.saveViewName}";
    }

    /**
     * 跳转到修改页面
     *
     * @param id    id
     * @param model 模型传输对象
     * @return 逻辑视图
     */
    @GetMapping(value = "/goUpdateView")
    public String goUpdateView(Integer id, Model model) {
        ${table.entityName} ${table.capitalEntityName} =  ${table.capitalServiceName}.get${table.entityName}ById(id);
        model.addAttribute("${table.capitalEntityName}", ${table.capitalEntityName});
        return prefixView + "${table.updateViewName}";
    }
    </#if>

    // 该类为示例返回格式，如没有特殊要求可以把该类单独创建一个类
    @Data
    static class ReturnResult implements Serializable {
        // 成功
        private static final Integer SUCCESS = 200;
        // 失败
        private static final Integer ERROR = 500;

        private Integer status;
        private Object data;
        private String msg;
        private Long count;

        public ReturnResult(Integer status, String msg) {
            this.status = status;
            this.msg = msg;
        }

        public ReturnResult(Integer status) {
            this.status = status;
        }

        public ReturnResult(Integer status, Object msg) {
            this.status = status;
            this.data = msg;
        }

        public ReturnResult(Integer status, Object obj, String msg) {
            this.status = status;
            this.data = obj;
            this.msg = msg;
        }

        public ReturnResult(Integer status, Object data, String msg, Long count) {
            this.status = status;
            this.data = data;
            this.msg = msg;
            this.count = count;
        }

        /**
         * 返回成功状态
         *
         * @param obj 需要传送的值
         * @return ReturnResult 对象
         */
        public static ReturnResult ok(Object obj) {
            return new ReturnResult(SUCCESS, obj);
        }

        /**
         * 返回成功状态
         *
         * @return ReturnResult 对象
         */
        public static ReturnResult ok() {
            return new ReturnResult(SUCCESS);
        }

        /**
         * 返回失败状态
         *
         * @param msg 失败消息
         * @return ReturnResult 对象
         */
        public static ReturnResult error(String msg) {
            return new ReturnResult(ERROR, msg);
        }

    }

    </#if>
}
